﻿Imports System.Net
Imports System.IO
Imports Newtonsoft.Json.Linq
Public Class objMessageReceived
    Public SenderID As Long
    Public Message As String
    Public Fullname As String
    Public Processing As Boolean = False
End Class
Public Class clsMessageUnread
    Dim frm As frmMain
    Dim info As objInfoLogin
    Dim ParseHTML As New clsParseHTML

    Public Sub New(ByRef frmMain As frmMain, ByRef infoLogin As objInfoLogin)
        frm = frmMain
        info = infoLogin
    End Sub
    Public Sub Callback(ByVal Status As String)
        If (Status = "Error") Then
            frm.Account.Login.infoLogin.AccessToken = ""
        End If
    End Sub

    Public Sub getUnread()
        frm.MessageFB.isCheckMessageUnread = True
        Dim MessageList As String = ParseHTML.getURL("https://graph.facebook.com/me/inbox/?fields=unread&access_token=" & frm.Account.Login.infoLogin.AccessToken, ErrorCallback:=AddressOf Callback)
        If MessageList <> "" Then
            Dim jMessageList As JObject = JObject.Parse(MessageList)
            Dim arrMessageList = jMessageList.SelectToken("data")
            Dim i As Integer = 0
            For i = 0 To arrMessageList.Count - 1
                Dim check As Integer = Convert.ToInt32(arrMessageList(i).SelectToken("unread"))
                If check > 0 Then
                    Dim id As Long = arrMessageList(i).SelectToken("id")
                    Dim sMessage = ParseHTML.getURL("https://graph.facebook.com/" & id.ToString & "?access_token=" & frm.Account.Login.infoLogin.AccessToken)
                    If sMessage <> "" Then
                        Dim jMessage As JObject = JObject.Parse(sMessage)
                        Dim Message = jMessage.SelectToken("comments").SelectToken("data")
                        Dim MessageTo = jMessage.SelectToken("to").SelectToken("data")
                        If Message.Count > 0 And MessageTo.Count < 3 Then
                            Dim LastMessage = Message(Message.Count - 1)
                            Dim MessageFrom As String = LastMessage.SelectToken("from").SelectToken("name")
                            Dim MessageId As String = LastMessage.SelectToken("from").SelectToken("id")
                            Dim MessageContent As String = LastMessage.SelectToken("message")
                            Dim obj As New objMessageReceived
                            obj.Fullname = MessageFrom
                            obj.Message = MessageContent
                            obj.SenderID = MessageId

                            If (frm.MessageFB.DictMessageUnread.ContainsKey(obj.SenderID) = False) Then
                                frm.MessageFB.DictMessageUnread.Add(obj.SenderID, obj)
                                With frm.lstMessageUnread.Items.Add(frm.lstMessageUnread.Items.Count + 1)
                                    .SubItems.Add(obj.Fullname)
                                    .SubItems.Add(obj.Message)
                                    .SubItems.Add(obj.SenderID)
                                End With
                            End If

                        End If
                    End If
                End If
            Next
        End If
        frm.MessageFB.isCheckMessageUnread = False
    End Sub
End Class
